package com.licq.ldoc.writer.visitor.example.exampler;

import com.licq.ldoc.domain.Element;
import com.licq.ldoc.utils.ClassUtils;
import com.licq.ldoc.writer.visitor.example.ExampleVisitor;
import com.licq.ldoc.writer.visitor.example.Exampler;
import com.lowagie.text.BadElementException;

/**
 * 
 * @author liujian
 *
 */
public class BasicAttributeElementExampler extends Exampler {
	
	private org.dom4j.Element domElement;
	
	private Object object;
	
	private Element element;

	public BasicAttributeElementExampler(org.dom4j.Element domElement,
			Object object, Element element) {
		this.domElement = domElement;
		this.object = object;
		this.element = element;
	}

	@Override
	public void create() throws BadElementException {
		String property = element.getName();
		Object propertyValue = getPropertyValue(object, property);
		if (propertyValue == null && element.isRequired()) {
			propertyValue = ClassUtils.safeNewInstance(element.getType().getClazz());
		}
		if (propertyValue != null) {
			org.dom4j.Element ele = domElement.addElement(property);
			element.accept(new ExampleVisitor(ele, propertyValue));
		}
	}

}
